<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Repositories\LanCompleteRepository;
use App\Repositories\OrderRepository;

class LanCompleteController extends AdminController
{

    public function __construct(LanCompleteRepository $lanComplete, OrderRepository $order)
    {
        $this->lanComplete = $lanComplete;
        $this->order = $order;
    }

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index(Request $request)
    {
        $orderId = $request->input('order_id');
        $data = $this->lanComplete->findAllBy('order_id', $orderId);

        $order = $this->order->find($orderId);

        $lans = config('system.language');
        $progress = config('system.lan_progress');

        if ($order->product == 2) {

        }

        return view('admin.lan.index', compact('data', 'lans', 'progress', 'orderId', 'order'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create(Request $request)
    {
        $orderId = $request->input('order_id');

        $lans = $this->lanComplete->getLanguageByOrder($orderId)->toArray();
        $language = collect(config('system.language'))->except($lans);

        $order = $this->order->find($orderId);

        return view('admin.lan.create', compact('orderId', 'language', 'order'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  Request  $request
     * @return Response
     */
    public function store(Request $request)
    {
        $result = $this->lanComplete->create($request->all());

        if ($result) {
            return redirect('admin/lan-complete?order_id='.$request->input('order_id'))->with('success', '语言版本添加成功'); 
        }

        return back()->with('error', '语言版本添加失败');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        $data = $this->lanComplete->find($id);

        return view('admin.lan.edit', compact('data'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  Request  $request
     * @param  int  $id
     * @return Response
     */
    public function update(Request $request, $id)
    {
        $result = $this->lanComplete->updateFind($request->all(), $id);

        $orderId = $this->lanComplete->find($id)->order_id;

        if ($result) {
            return redirect('admin/lan-complete?order_id='.$orderId)->with('success', '语言版本进度更新成功'); 
        }

        return back()->with('error', '语言版本进度更新失败');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy(Request $request, $id)
    {
        $result = $this->lanComplete->delete($id);

        if ($result) {
            flash('success', '语言版本删除成功');
            return response()->json(['code' => 200]);
        }

        flash('error', '语言版本删除失败');
        return response()->json(['code' => 500]);
    }

    /**
     * 进度更新
     *
     * @param  int  $id        语言版本的ID号
     * @param  int  $progress  要设置的进度
     * @return Response
     */ 
    public function progressUpdate(Request $request, $id, $progress)
    {
        $data = $request->all();

        $currentLan = $this->lanComplete->find($id);

        // 更新当前语言的进度
        $this->lanComplete->update(['progress'=>$progress], $id);

        // 当前语言版本所在订单的所有语言'order_id', 
        $otherLans = $this->lanComplete->findWhere([
            'order_id' => $currentLan->order_id,
            ['id', '!=', $id]
        ]);

        // 判断当前语言是否为主语言（或者为第一个完成的语言）
        if ($otherLans->isEmpty()) {
            $isMain = true;
        } else {
            $isMain = ($progress > $otherLans->max('progress')) ?: false;  
        }

        // 如果为主语言则相关操作将更新对应订单的时间
        $currentLan->progress = $progress;
        $isMain && \Event::fire(new \App\Events\lanProgressChange($currentLan));

        return back()->with('success', '语言版本进度更新成功！');
    }
}